﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using YiDingCanWp7.Models;

namespace YiDingCanWp7
{
    public partial class AppFeedback : PhoneApplicationPage
    {
        public AppFeedback()
        {
            InitializeComponent();
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(ContentTextBox.Text.Trim()))
            {
                MessageBox.Show("请输入建议内容");
                return;
            }
            performanceProgressBar.IsIndeterminate = true;
            var webClient = new WebClient();
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
            ShopFeedback feedback = new ShopFeedback()
            {
                Content = ContentTextBox.Text.Trim(),
                Contact = EmailTextBox.Text.Trim(),
                DeviceUniqueId = PhoneUtils.GetDeviceUniqueId()
            };
            var json = JsonConvert.SerializeObject(feedback);
            webClient.UploadStringAsync(new Uri(BaseConfig.AddFeedbackUrl,UriKind.RelativeOrAbsolute), "POST", json);
        }
        void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                MessageDTO message = JsonConvert.DeserializeObject<MessageDTO>(e.Result);
                MessageBox.Show(message.message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                performanceProgressBar.IsIndeterminate = false;
            }
        }
    }
}